package com.martinwj.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.martinwj.entity.Group;
import com.martinwj.entity.Result;
import com.martinwj.entity.User;
import com.martinwj.exception.SysException;
import com.martinwj.service.GroupService;
import com.martinwj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2021-04-05
 */
@Controller
@RequestMapping("admin/user")
public class AdminUserAction {
    @Autowired
    private UserService userService;
    @Autowired
    private GroupService groupService;

    /**
     * 查询所有用户（正常）
     */
    @RequestMapping("list_normal.action")
    public String listNormal(ModelMap map,
                             @RequestParam(required=false, value="groupId") String groupId,
                             @RequestParam(required=false, value="keyWord") String keyWord,
                             @RequestParam(value="pageNum", defaultValue="1") int pageNum,
                             @RequestParam(value="pageSize", defaultValue="10") int pageSize) {

        Map<String, Object> param = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(groupId)) {
            param.put("groupId", groupId);
            map.put("groupId", groupId);
        }
        if (!StringUtils.isEmpty(keyWord)) {
            param.put("keyWord", keyWord.trim());
            map.put("keyWord", keyWord);
        }
        param.put("status", "1");

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.list(param);
        PageInfo<User> page = new PageInfo<User>(list);
        map.put("pageInfo", page);

        // 查询所有用户组
        List<Group> groupList = groupService.list();
        map.put("groupList", groupList);

        return "admin/user_info/list_normal";
    }

    /**
     * 查询所有用户（未激活）
     */
    @RequestMapping("list_not_active.action")
    public String listNotActive(ModelMap map,
                                @RequestParam(required=false, value="groupId") String groupId,
                                @RequestParam(required=false, value="keyWord") String keyWord,
                                @RequestParam(value="pageNum", defaultValue="1") int pageNum,
                                @RequestParam(value="pageSize", defaultValue="10") int pageSize) {

        Map<String, Object> param = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(groupId)) {
            param.put("groupId", groupId);
            map.put("groupId", groupId);
        }
        if (!StringUtils.isEmpty(keyWord)) {
            param.put("keyWord", keyWord.trim());
            map.put("keyWord", keyWord);
        }
        param.put("status", "0");

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.list(param);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        map.put("pageInfo", pageInfo);

        // 查询所有用户组
        List<Group> groupList = groupService.list();
        map.put("groupList", groupList);

        return "admin/user_info/list_not_active";
    }

    /**
     * 查询所有用户（封禁）
     */
    @RequestMapping("list_ban.action")
    public String listBan(ModelMap map,
                          @RequestParam(required=false, value="groupId") String groupId,
                          @RequestParam(required=false, value="keyWord") String keyWord,
                          @RequestParam(value="pageNum", defaultValue="1") int pageNum,
                          @RequestParam(value="pageSize", defaultValue="10") int pageSize) {

        Map<String, Object> param = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(groupId)) {
            param.put("groupId", groupId);
            map.put("groupId", groupId);
        }
        if (!StringUtils.isEmpty(keyWord)) {
            param.put("keyWord", keyWord.trim());
            map.put("keyWord", keyWord);
        }
        param.put("status", "2");

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.list(param);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        map.put("pageInfo", pageInfo);

        // 查询所有用户组
        List<Group> groupList = groupService.list();
        map.put("groupList", groupList);

        return "admin/user_info/list_ban";
    }

    /**
     * 批量更新用户状态
     * @param idArr 用户id数组
     * @param status 状态
     * @return
     */
    @RequestMapping("batch_update_status.json")
    @ResponseBody
    public Result batchUpdateStatus(
            @RequestParam(value = "idArr") String[] idArr,
            @RequestParam(value = "status") String status) {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("idArr", idArr);
        param.put("status", status);

        userService.batchUpdateStatus(param);

        return Result.success();
    }

    /**
     * 批量更换用户所在用户组
     * @param idArr 用户id数组
     * @param groupId 用戶組id
     * @return
     */
    @RequestMapping("batch_change_group.json")
    @ResponseBody
    public Result batchChangeGroup(
            @RequestParam(value = "idArr") String[] idArr,
            @RequestParam(value = "groupId") String groupId) {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userIdArr", idArr);
        param.put("groupId", groupId);

        userService.batchChangeGroup(param);

        return Result.success();
    }

    /**
     * 批量删除封禁用户的内容
     * @param idArr 用户id数组
     * @param deleteArr 删除内容表名数组
     * @return
     */
    @RequestMapping("batch_delete_content.json")
    @ResponseBody
    public Result batchDeleteContent(
            @RequestParam(value = "idArr") String[] idArr,
            @RequestParam(value = "deleteArr") String[] deleteArr) {

        userService.batchDeleteContent(idArr, deleteArr);

        return Result.success();
    }

    /**
     * 用户编辑
     * @param map
     * @param id 用户id
     * @return
     */
    @RequestMapping("edit.action")
    public String edit(ModelMap map,
                       @RequestParam(required = false, value = "id") String id) {

        // 查询单个用户的信息
        if (!StringUtils.isEmpty(id)) {
            User userInfo = userService.selectById(id);
            // 此处有BUG，不能使用userInfo，否则id为空时，会默认获取管理员的信息，导致无法添加新用户
            map.put("editUserInfo", userInfo);
        }

        // 查询所有用户组
        List<Group> groupList = groupService.list();
        map.put("groupList", groupList);

        map.put("id", id);
        return "admin/user_info/edit";
    }

    /**
     * 保存用户信息
     * @param user
     * @return
     * @throws SysException
     */
    @RequestMapping("save.json")
    @ResponseBody
    public Result save(User user) throws SysException {

        userService.save(user);

        return Result.success();
    }
}
